 /*******************************************************************************************
 *	         kmalloc.h
 ********************************************************************************************/
#ifndef KMALLOC_H_
#define KMALLOC_H_

/*********************************************************************************************
*	global function prototype
*********************************************************************************************/

/**
 * <pre>
 *   void *kmalloc(int size);
 * </pre>
 *
 * \brief Allocate a block from heap memory.
 *
 * This functions allocates a memory block of the specified
 * size and returns a pointer to that block.
 *
 * The actual size of the allocated block is larger than the
 * requested size because of space required for maintenance
 * information. This additional information is invisible to
 * the application.
 *
 * The routine looks for the smallest block that will meet
 * the required size and releases it to the caller. If the
 * block being requested is usefully smaller than the smallest
 * free block then the block from which the request is being
 * met is split in two. The unused portion is put back into
 * the free-list.
 *
 * The contents of the allocated block is unspecified.
 * To allocate a block with all bytes set to zero use
 * kmalloc_clear().
 *
 * \note Interrupts are automatically enabled, when this
 *       function returns.
 *
 * \param size[in] Size of the requested memory block.
 *
 * \return pointer to the allocated memory block if the
 *         function is successful or NULL if fail.
 * \idef IM_HEAP_KALLOC
 */
void *kmalloc(int size);


/**
 * <pre>
 *   void *kmalloc(int size);
 * </pre>
 *
 * \brief Allocate a block from heap memory with preset 0
 *
 * \param size[in] size of the requested memory block
 *
 * \return pointer to the allocated memory block if success, 
 *         otherwise return NULL
 *
 * \idef IM_HEAP_KALLOC_CLEAR
 */
void *kmalloc_clear(int size);


/** 
 * <pre>
 *  void *krealloc(void *block, int size);
 * </pre>
 *
 * \brief realloc memory from kernel heap. this function will copy
 *        the content from old memory block to new alloced memory,
 *        and then free the old memory block if success.
 *
 * \param block[in] the previous allocated block pointer
 * \param size[in] size of new requested memory
 *
 * \return pointer to the new allocated memory block if success,
 *         otherwise return NULL
 * \idef IM_HEAP_KREALLOC
 */
void *krealloc(void *block, int size);


/** 
 * <pre>
 *  void *kcalloc(int num, int size);
 * </pre>
 *
 * \brief alloc array of memory block from kernel heap
 *
 * \param num[in] array members
 * \param size[in] size of each requested memory block
 *
 * \return pointer to the new allocated memory block array if success,
 *         otherwise return NULL
 * \idef IM_HEAP_KCALLOC
 */
void *kcalloc(int num, int size);

/** 
 * <pre>
 *  int kfree(void *block);
 * </pre>
 *
 * \brief return the allocated memory block back to kernel heap
 *
 * \param block[in] pointer of memory block which allocated from kernel heap
 *
 * \return 0 if success, otherwise return nagetive value
 *
 * \idef IM_HEAP_KFREE
 */
int kfree(void *block);


/** 
 * <pre>
 *  int kheap_total(void);
 * </pre>
 *
 * \brief get the total size of kernel heap
 *
 * \return size of kernel heap
 *
 * \idef IM_HEAP_TOTAL
 */
int kheap_total(void);

/** 
 * <pre>
 *  int kheap_available(void);
 * </pre>
 *
 * \brief get current available kernel heap size
 *
 * \return available size of kernel heap
 *
 * \idef IM_HEAP_AVAILABLE
 */
int kheap_available(void);

/** 
 * <pre>
 *  int kheap_minimal(void);
 * </pre>
 *
 * \brief get historical minimum available kernel heap size
 *
 * \return historical minimum available kernel heap size in bytes
 *
 * \idef IM_HEAP_MIN
 */
int kheap_minimal(void);

/** 
 * <pre>
 *  int kheap_check(void);
 * </pre>
 *
 * \brief check the kernel heap space.
 *
 * \return > 0 if success, otherwise return negative value.
 *
 * \idef IM_HEAP_CHECK
 */
int kheap_check(void);


int MemoryInit(uint32_t stack_start, uint32_t stack_end);

int cmdKminsp(void);

#endif //KMALLOC_H


